import os
import re


class FindNewCustomer(object):

    def get_file(self, path):
        file_list = []
        for root, dir, files in os.walk(path):
            for file in files:
                file_abs = rf'{root}\{file}'
                if file[-3:] == 'xml':
                    file_list.append(file_abs)
        return file_list

    def get_data(self, file_path):
        f = open(file_path, 'r', encoding='utf8')
        lines = f.readlines()
        for line in lines:
            if "<bIsNewCustomer>-1</bIsNewCustomer>" in line:
                f.close()
                f1 = open(file_path, 'r', encoding='utf8')
                line1 = f1.read()
                lRetailStoreID = re.findall(r'<lRetailStoreID>(.*)</lRetailStoreID>', line1)[0]
                lWorkstationNmbr = re.findall(r'<lWorkstationNmbr>(.*)</lWorkstationNmbr>', line1)[0]
                lTaNmbr = re.findall(r'<lTaNmbr>(.*)</lTaNmbr>', line1)[0]
                szCustomerId = re.findall(r'<szCustomerId>(.*)</szCustomerId>', line1)[0]
                szMobilePhone = re.findall(r'<szMobilePhone>(.*)</szMobilePhone>', line1)[0]
                fw = open(r'../02-找数据/data.txt', 'a', encoding='utf8')
                fw.write(f'{lRetailStoreID},{lWorkstationNmbr},{lTaNmbr},{szCustomerId},{szMobilePhone}\n')
                fw.close()
                f1.close()
                if int(lWorkstationNmbr) < 10:
                    return "pos"
                elif int(lWorkstationNmbr) >= 90:
                    return "aco"


if __name__ == '__main__':
    find = FindNewCustomer()
    path = input(r'Please input dir: ')
    # path = r'D:\Disktop\新建文件夹 (4)'
    file_list = find.get_file(path)

    pos_no = 0
    aco_no = 0
    for file_path in file_list:
        if find.get_data(file_path) == "pos":
            pos_no = pos_no + 1
        elif find.get_data(file_path) == "aco":
            aco_no = aco_no + 1

    print(pos_no)
    print(aco_no)